﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;


namespace MADSite
{
  public partial class ReturnTickets : System.Web.UI.Page
  {
    private int id = -1;
    private string ticketId;
    TrainManagerClient trainManagerClient = new TrainManagerClient();
    protected void Page_Load(object sender, EventArgs e)
    {
      if (!Request.Cookies.AllKeys.Contains("id"))
      {
        id = trainManagerClient.InitUser();
      }
      else
      {
        id = Int32.Parse(Request.Cookies["id"].Value);
      }
      HttpCookie cookie = new HttpCookie("id", id.ToString());
      Response.Cookies.Add(cookie);

      //routeTextBox.Text += sessionId;
      cassirBox.Checked = trainManagerClient.isCashier(id);
      cassirBox_CheckedChanged(sender, e);      
    }
    
    /// <summary>
    /// Возвращаем билет
    /// </summary>
    /// <param name="sender"></param>
    /// <param name="e"></param>
    protected void returnButton_Click(object sender, EventArgs e)
    {
      //TODO: сделать запрос в БД на удаление нужных данных
      try
      {
       trainManagerClient.ReturnTicket(id, tbLogicalId.Text);
      }
      catch
      {

      }
    }

    /// <summary>
    /// Если установить флаг, появится опция возврата, если убрать флаг, то она исчезнет
    /// </summary>
    /// <param name="sender"></param>
    /// <param name="e"></param>
    protected void cassirBox_CheckedChanged(object sender, EventArgs e)
    {
      if (cassirBox.Checked)
      {
        Label1.Visible = true;
        Label2.Visible = true;
        Label3.Visible = true;
        Label4.Visible = true;
        Label5.Visible = true;
        Label6.Visible = true;
        Label7.Visible = true;
        routeTextBox.Visible = true;
        numberTextBox.Visible = true;
        placeTextBox.Visible = true;
        timeTextBox.Visible = true;
        fioTextBox.Visible = true;
        passportTextBox.Visible = true;
        returnButton.Visible = true;
      }
      else
      {
        Label1.Visible = false;
        Label2.Visible = false;
        Label3.Visible = false;
        Label4.Visible = false;
        Label5.Visible = false;
        Label6.Visible = false;
        Label7.Visible = false;
        routeTextBox.Visible = false;
        numberTextBox.Visible = false;
        placeTextBox.Visible = false;
        timeTextBox.Visible = false;
        fioTextBox.Visible = false;
        passportTextBox.Visible = false;
        returnButton.Visible = false;
      }
    }

    protected void Button1_Click(object sender, EventArgs e)
    {
      ticketId = tbLogicalId.Text;
      Mads.CoreDomain.TicketToReturn ticket = 
        trainManagerClient.GetTicketByNumber(id, tbLogicalId.Text);
      if (ticket == null)
        return;
      placeTextBox.Text = ticket.SitNumber.ToString();
      routeTextBox.Text = ticket.RouteNumber.ToString();
      //timeTextBox.Text = ticket.StartStation.DepartureTime.ToString();
      numberTextBox.Text = ticket.CarNumber.ToString();
      
    }
  }
}